
let IDCache = -1;

const CreateID = () => {
    return IDCache--;
}

const EachObject = (Obj, callback) => {
    for (const key in Obj) {
        if (Object.prototype.hasOwnProperty.call(Obj, key)) {
            const element = Obj[key];
            callback(element, key);
        }
    }
}

// 执行判断条件的方法
export const StringToRule = (string, Object) => {
    var keyArray = [];
    var ValueArray = [];
    for (const key in Object) {
        keyArray.push(key);
        ValueArray.push(Object[key]);
    };
    try {
        return !!new Function(keyArray.join(','), 'return ' + string).apply(null, ValueArray);
    } catch (error) {
        console.error('Rule 字段配置错误', error);
    }
}


export const FormatValue = (ParamConfig, DefaultValue) => {
    var Value = {};
    ParamConfig.forEach(element => {
        if (DefaultValue && DefaultValue.hasOwnProperty(element.Key)) {
            Value[element.Key] = DefaultValue[element.Key]
        } else if (element.hasOwnProperty('Default')) {
            Value[element.Key] = element.Default
        } else {
            switch (element.Type) {
                case 'String':
                    Value[element.Key] = '';
                    break;
                case 'Number':
                    Value[element.Key] = 0;
                    break;
                case 'Int':
                    Value[element.Key] = 0;
                    break;
                case 'Primary':
                    Value[element.Key] = CreateID();
                    break;
                default:
                    Value[element.Key] = null;
                    break;
            }
        }
    });
    return { ...DefaultValue, ...Value }
}


export const GetChangeValueEvents = (ParamConfig) => {
    var Events = {};
    ParamConfig.forEach(element => {
        if (element.Event && element.Event.Value) {
            Events[element.Key] = element.Event.Value;
        }
    });
    return Events;
}

export const GetChangeOptionEvents = (ParamConfig) => {
    var Events = {};
    ParamConfig.forEach(element => {
        if (element.Event && element.Event.Option) {
            Events[element.Key] = element.Event.Option;
        }
    });
    return Events;
}

export const GetParamsMapping = (ParamConfig) => {
    const Mapping = {};
    ParamConfig.forEach((Item) => {
        Mapping[Item.Key] = Item;
    });
    return Mapping
};


export const GetDictMapping = (ParamConfig) => {
    const Mapping = {};
    ParamConfig.forEach((Item) => {
        if (Item.hasOwnProperty('Dict') && !Array.isArray(Item.Dict)) {
            const DictValue = Item.Dict.split(':');
            if (DictValue.length == 3) {
                const [Parentkey, Child, Default] = DictValue;
                if (!Mapping[Parentkey]) {
                    Mapping[Parentkey] = {}
                }
                Mapping[Parentkey][Item.Key] = {
                    Key: Child,
                    Default: Default
                }
            }
        }
    });
    return Mapping
}


export const GetDefaultFormView = (ParamMapping, FormConfig) => {
    if (!FormConfig) return [];
    var View = [];
    for (let i = 0; i < FormConfig.length; i++) {
        const element = FormConfig[i];
        const FormItem = {
            title: element.title,
            labelCol: 150,
            children: [],
        }
        for (let d = 0; d < element.children.length; d++) {
            const item = element.children[d];
            const ItemView = item.split(':');
            if (ParamMapping[ItemView[1]] && ItemView.length == 2) {
                ParamMapping[ItemView[1]].ViewType = ItemView[0];
                ParamMapping[ItemView[1]] && FormItem.children.push(ParamMapping[ItemView[1]]);
            } else if (ParamMapping[item]) {
                FormItem.children.push(ParamMapping[item]);
            }
        };
        View.push(FormItem);
    }
    return View;
}